package com.delos.biz.service;

import com.alibaba.fastjson.JSON;
import com.delos.api.bo.UserBO;
import com.delos.api.dao.IUserDao;
import com.delos.api.entity.UserDO;
import com.delos.api.service.IUserService;
import com.delos.rpc.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author zouyaowen
 * @date 2020-05-17
 */
@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public UserBO getUser(Long userId) {
        Optional<UserDO> userOptional = userDao.getUser(userId);
        UserDO userDO = userOptional.orElse(null);
        if (userDO != null) {
            UserBO userBO = new UserBO();
            BeanUtils.copyProperties(userDO, userBO);
            return userBO;
        }
        return null;
    }

    @Override
    public void addUser(UserDTO userDTO) {
        log.info("userDTO=[{}]", JSON.toJSONString(userDTO));
        userDao.addUser(userDTO);
    }

    @Override
    public UserBO getUserCache(Long userId) {
        Optional<UserDO> userOptional = userDao.getUserCache(userId);
        UserDO userDO = userOptional.orElse(null);
        if (userDO != null) {
            UserBO userBO = new UserBO();
            BeanUtils.copyProperties(userDO, userBO);
            return userBO;
        }
        return null;
    }

    @Override
    public void modifyUserCache(UserDTO userDTO) {
        userDao.updateUserCache(userDTO);
    }

    @Override
    public void removeUserCache(Long userId) {
        userDao.deleteUserCache(userId);
    }
}

